package com.platform.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.google.common.collect.Lists;
import com.platform.annotation.SysLog;
import com.platform.common.ServerResponse;
import com.platform.dao.MenuMapper;
import com.platform.pojo.Menu;
import com.platform.service.IMenuService;
import com.platform.util.NumberUtil;
import com.platform.util.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.util.*;

@Service("iMenuService")
public class MenuServiceImpl implements IMenuService {

    @Autowired
    private MenuMapper menuMapper;

    @SysLog("新增菜单")
    public ServerResponse<String> insert(Menu menu){
        int countResult = menuMapper.insert(menu);
        if(null == menu.getParentId()){
            menu.setParentId(0);
        }
        if(countResult > 0){
            return ServerResponse.createBySuccessMessage("操作成功");
        }
        return ServerResponse.createByErrorMessage("操作失败");
    }

    /*@CacheEvict(value = "findAllUserData",allEntries=true)*/
    @SysLog("删除菜单")
    public ServerResponse<String> del(Integer id){
        int countResult = menuMapper.deleteByPrimaryKey(id);
        if(countResult > 0){
            return ServerResponse.createBySuccessMessage("操作成功");
        }
        return ServerResponse.createByErrorMessage("操作失败");
    }

    @SysLog("更新菜单信息")
    public ServerResponse<String> update(Menu menu){
        if(NumberUtil.safeToNumber(menu.getId()) == 0){
            return ServerResponse.createByErrorMessage("数据错误");
        }
        int countResult = menuMapper.updateByPrimaryKeySelective(menu);
        if(countResult > 0){
            return ServerResponse.createBySuccessMessage("操作成功");
        }
        return ServerResponse.createByErrorMessage("操作失败");
    }

    @SysLog("查询用户已有菜单权限")
    public ServerResponse<List<Map>> selectByUserName(String userName,Integer parentId){
        return ServerResponse.createBySuccess("查询成功",getChildMenuList(userName,parentId));
    }

    /**
     * 查询子菜单
     * @param parentId
     * @return
     */
    /*@Cacheable("findAllUserData")*/
    @SysLog("查询父节点已有子节点")
    List<Map> getChildMenuList(String userName,Integer parentId){
        List<Map> resultList = new LinkedList<Map>();
        List<Map> menuLinkedList = menuMapper.selectByUserNameAndParentId(userName,parentId);
        if(menuLinkedList != null){
            for (Map map : menuLinkedList) {
                Map resultMap = new HashMap();
                resultMap.put("id",map.get("id"));
                resultMap.put("parent_id",map.get("parent_id"));
                resultMap.put("url",map.get("url"));
                resultMap.put("name",map.get("name"));
                resultMap.put("state",map.get("state"));
                resultMap.put("link_type",map.get("link_type"));
                if(menuMapper.countByUserNameAndParentId(userName,NumberUtil.safeToNumber(map.get("id"))) > 0){
                    resultMap.put("children",getChildMenuList(userName,NumberUtil.safeToNumber(map.get("id"))));//子节点
                }
                resultList.add(resultMap);
            }
        }
        return resultList;
    }


    /**
     * 此处为查询所有的菜单并将角色ID已有权限的菜单赋值have为1
     * @param roleCode
     * @param parentId
     * @return
     */
    List<Map> getChildMenuList2(String roleCode,Integer parentId){
        List<Map> resultList = new LinkedList<Map>();
        //查询出所有的菜单节点
        List<Map> menuLinkedList = menuMapper.selectAllByAndparentId(parentId);
        //查询出角色已有权限的菜单ID
        String menuIdArr = menuMapper.selectByRoleCode(roleCode)+",";
        if(menuLinkedList != null){
            for (Map map : menuLinkedList) {
                    Map resultMap = new HashMap();
                    resultMap.put("id",map.get("id"));
                    resultMap.put("parent_id",map.get("parent_id"));
                    resultMap.put("url",map.get("url"));
                    resultMap.put("name",map.get("name"));
                    resultMap.put("state",map.get("state"));
                    if(menuIdArr.contains(StringUtil.valueOfString(map.get("id"))+",")){
                        resultMap.put("have","1");
                    }else{
                        resultMap.put("have","2");
                    }
                    if(menuMapper.selectAllByAndparentId(NumberUtil.safeToNumber(map.get("id"))).size() > 0){
                        resultMap.put("children",getChildMenuList2(roleCode,NumberUtil.safeToNumber(map.get("id"))));//子节点
                    }
                    resultList.add(resultMap);
            }
        }
        return resultList;
    }

    /**
     * 供角色管理配置菜单查询
     * @param roleCode
     * @param parentId
     * @return
     */
    @SysLog("查询特定格式菜单")
    public ServerResponse<List<Map>> selectAllMenu(String roleCode,Integer parentId){
        //返回的List
        List<Map> resultList = Lists.newArrayList();
        //查询用户拥有权限的菜单
        List<Map> enumList = getChildMenuList2(roleCode,parentId);
        //循环改值
        if(null != enumList && 0 < enumList.size()){
            for (int i = 0; i < enumList.size(); i++) {
                //返回母节点Map
                Map map = new Hashtable();
                map.put("text",enumList.get(i).get("name"));
                map.put("href","");
                map.put("have",enumList.get(i).get("have"));
                map.put("tags",enumList.get(i).get("id"));
                //母节点下子节点
                List<Map> nodeList =(List<Map>) enumList.get(i).get("children");
                if(null != nodeList && 0 < nodeList.size()){
                    //返回子节点集合
                    List<Map> childList = Lists.newArrayList();
                    //循环改子节点值
                    for (int j = 0; j < nodeList.size(); j++) {
                        //子节点集合
                        Map childMap = new Hashtable();
                        childMap.put("text",nodeList.get(j).get("name"));
                        childMap.put("href","");
                        childMap.put("have",nodeList.get(j).get("have"));
                        childMap.put("tags",nodeList.get(j).get("id"));
                        childList.add(childMap);
                    }
                    map.put("nodes",childList);
                }
                resultList.add(map);
            }
        }
        return ServerResponse.createBySuccess("查询成功",resultList);
    }

    @SysLog("根据条件查询菜单")
    public ServerResponse<PageInfo> selectByParam(Map param){
        int pageNo = NumberUtil.safeToNumber(param.get("pageNo"),1);
        int pageSize = NumberUtil.safeToNumber(param.get("pageSize"),10);
        String order = StringUtil.valueOfString(param.get("order"));
        PageHelper.startPage(pageNo,pageSize,order);
        List<Map> resultList = menuMapper.selectByParam(param);
        PageInfo pageInfo = new PageInfo(resultList);
        pageInfo.setList(resultList);
        return ServerResponse.createBySuccess("查询成功",pageInfo);
    }

    @SysLog("加载所有一级菜单")
    public ServerResponse<List<Map>> selectAllFirstMenu(){
        List<Map> resultList = menuMapper.selectAllFirstMenu();
        if(null != resultList && 0 < resultList.size()){
            return ServerResponse.createBySuccess("查询成功",resultList);
        }
        return ServerResponse.createByErrorMessage("查询失败");
    }
}
